job "echo" {
  datacenters = ["dc1"]
  type        = "service"

  # 启用自动扩缩容策略
  update {
    max_parallel = 2
    min_healthy_time = "10s"
    healthy_deadline = "3m"
  }

  group "echo" {
    count = 3  # 初始实例数

    # 动态端口配置（避免冲突）
    network {
      port "http" {
        to     = 9999  # 容器内固定端口
      }
      port "metrics" {
        to     = 9100  # Prometheus 指标端口
        static = 9100  # 固定端口（便于 Prometheus 抓取）
      }
    }

    service {
      name = "api-service"
      port = "http"  # 动态端口注册到 Consul
      provider = "consul"

      check {
        type     = "http"
        path     = "/health"
        interval = "10s"
        timeout  = "2s"
      }

      # 添加 Prometheus 指标标签
      tags = [
        "metrics_port=9100",
        "metrics_path=/metrics"
      ]
    }

    task "echo" {
      driver = "docker"

      env {
        CONSUL_ADDR = "10.1.12.4:8500"
      }

      config {
        image = "registry.cn-hangzhou.aliyuncs.com/my_public_images/echo:latest"  # 替换为你的镜像
        ports = ["http", "metrics"]      # 暴露端口
      }

      # 资源限制（根据监控数据调整）
      resources {
        cpu    = 100  # 100 MHz
        memory = 64  # 64 MB
      }

      # 环境变量注入动态端口
      env {
        API_PORT = "${NOMAD_PORT_http}"  # 动态端口传递给应用
      }
    }
  }
}